package com.snail.readyvr.activity;

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.snail.readyvr.R;
import com.snail.readyvr.application.MyApplication;
import com.snail.readyvr.di.component.DaggerAddVideoActivityPresenterComponent;
import com.snail.readyvr.di.module.AddVideoActivityPresenterModule;
import com.snail.readyvr.fragment.AddVideoFragment;
import com.snail.readyvr.fragment.VideoHistoryFragment;
import com.snail.readyvr.mvp.presenter.AddVideoActivityPresenter;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;


/**
 * Created by MRKING on 2016/11/17.
 */

public class AddVideoActivity extends CheckPermissionsActivity {
    @BindView(R.id.add_new_video_title)
    TextView mAddNewVideoTitle;
    @BindView(R.id.add_video_text)
    TextView mAddVideoText;

    @BindView(R.id.add_video_layout)
    LinearLayout mContent;

    @Inject
    AddVideoActivityPresenter mPresenter;
    private boolean isAddPage = true;

    private AddVideoFragment mAddVideoFragment;
    private VideoHistoryFragment mVideoHistoryFragment;

    @Override
    protected void hasGetAllPermissions() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_add_video;
    }

    @Override
    protected void init() {

        mAddVideoFragment = new AddVideoFragment();
        mVideoHistoryFragment = new VideoHistoryFragment();
        DaggerAddVideoActivityPresenterComponent
                .builder()
                .applicationComponent(((MyApplication)getApplication()).getApplicationComponent())
                .addVideoActivityPresenterModule(new AddVideoActivityPresenterModule(mAddVideoFragment, mVideoHistoryFragment))
                .build().inject(this);

        FragmentTransaction ft = getFragmentTransaction();
        ft.add(R.id.add_new_video_content,mAddVideoFragment);
        ft.add(R.id.add_new_video_content,mVideoHistoryFragment);
        ft.show(mAddVideoFragment);
        ft.hide(mVideoHistoryFragment);
        ft.commit();

    }

    private void changeFragment() {
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mContent.getLayoutParams();
        isAddPage = !isAddPage;
        //mAddVideoImg.setBackgroundResource(isAddPage?R.drawable.history:R.drawable.delete);
        mAddVideoText.setTextColor(isAddPage?getResources().getColor(R.color.seekbar_progress_color):getResources().getColor(R.color.video_history_list_delete_color));
        mAddVideoText.setText(isAddPage?R.string.new_video_history_title:R.string.delete_ok_tip);
        mAddNewVideoTitle.setText(isAddPage?R.string.add_new_video_title:R.string.new_video_history_title);
        FragmentTransaction ft = getFragmentTransaction();
        ft.show(isAddPage?mAddVideoFragment:mVideoHistoryFragment);
        ft.hide(isAddPage?mVideoHistoryFragment:mAddVideoFragment);

        params.height = isAddPage?ViewGroup.LayoutParams.WRAP_CONTENT:ViewGroup.LayoutParams.MATCH_PARENT;
        params.width = ViewGroup.LayoutParams.MATCH_PARENT;

        ft.commit();

        mContent.setLayoutParams(params);
    }

    private FragmentTransaction getFragmentTransaction() {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        return ft;
    }

    @OnClick({R.id.add_video_back, R.id.add_video_right_layout})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.add_video_back:
                if (!isAddPage)
                    changeFragment();
                else
                  finish();
                break;
            case R.id.add_video_right_layout:
                if (isAddPage)
                    changeFragment();
                else {
                    mVideoHistoryFragment.deleteAll();
                }
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK && !isAddPage){
            changeFragment();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

}
